package com.unlcn.ils.kas.controller;

import com.google.common.base.Strings;
import com.unlcn.ils.kas.annotation.LogRecord;
import com.unlcn.ils.kas.dao.model.sysadmin.bo.AccountUserBo;
import com.unlcn.ils.kas.service.account.AccountService;
import com.unlcn.ils.kas.service.security.config.JwtSettings;
import com.unlcn.ils.kas.vo.RestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by qichao on 2017/12/12.
 */
@Controller
@RequestMapping(value = "/account", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

public class AccountController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private JwtSettings jwtSettings;

    @LogRecord(description = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public RestVO<Map<String, String>> formLogin(@RequestParam("username") String username,
            @RequestParam("password") String password) throws Exception {

        String accountCode = username;

        LOGGER.info("UserController.userLogin params : {}, {}", accountCode, password);
        RestVO<Map<String, String>> result = new RestVO<>(0, null, "success");

        try {
            String token = accountService.accountLogin(username, password);
            if (Strings.isNullOrEmpty(token)) {
                result.setCode(10003);
                result.setMessage("用户名或口令错误");
            } else {
                Map<String, String> tokenMap = new HashMap<String, String>();
                tokenMap.put(jwtSettings.getHeader(), token);
                result.setData(tokenMap);
            }
        } catch (Exception e) {
            LOGGER.error("AccountController.login ERROR Message : {} ", e.getMessage());
            result.setCode(10003);
            result.setMessage("用户名或口令错误");
        }
        return result;
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    // @PreAuthorize("")
    public RestVO<AccountUserBo> accountInfo() throws Exception {
        RestVO<AccountUserBo> result = new RestVO<>(0, null, "success");
        try {
            AccountUserBo accountUserBo = accountService.getAccountInfo();
            if (Objects.equals(accountUserBo, null)) {
                result.setCode(10003);
                result.setMessage("用户名或口令错误");
            } else {
                result.setData(accountUserBo);
            }
        } catch (Exception e) {
            LOGGER.error("AccountController.accountInfo ERROR Message : {} ", e.getMessage());
            result.setCode(10003);
            result.setMessage("用户名或口令错误");
        }
        return result;
    }

    @LogRecord(description = "用户退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public RestVO<Boolean> accountLogout() throws Exception {
        RestVO<Boolean> result = new RestVO<>(0, null, "success");
        Boolean blnRtn = false;

        try {
            blnRtn = accountService.accountLogout();
            result.setData(blnRtn);

        } catch (Exception e) {
            LOGGER.error("AccountController.accountLogout ERROR Message : {} ", e.getMessage());
            result.setCode(10003);
            result.setMessage("用户名或口令错误");
        }
        return result;
    }

    @GetMapping(value = "/examinPassword")
    @ResponseBody
    public RestVO<Boolean> examinPassword(@RequestParam("password") String password) throws Exception {
        RestVO<Boolean> result = new RestVO<>(0, null, "success");
        Boolean blnRtn = false;

        try {
            blnRtn = accountService.examinPassword(password);
            result.setData(blnRtn);
        } catch (Exception e) {
            LOGGER.error("AccountController.examinePassword ERROR Message : {} ", e.getMessage());
            result.setCode(10003);
            result.setData(blnRtn);
            result.setMessage("旧密码错误");
        }
        return result;
    }

    @PostMapping(value = "/updatePwd")
    @ResponseBody
    public RestVO<Boolean> updatePwd(@RequestParam("password") String password) throws Exception {
        RestVO<Boolean> result = new RestVO<>(0, null, "success");

        try {
            boolean blnRtn = accountService.updatePwd(password);
            result.setData(blnRtn);
        } catch (Exception e) {
            LOGGER.error("AccountController.updatePwd ERROR Message : {} ", e.getMessage());
            result.setCode(10003);
            result.setMessage("修改密码错误");
        }
        return result;
    }
}
